﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Fifteen
{
    public class TButton : Button
    {
        private bool active = false;
        private Point position = Point.Empty;
        private Point finalPosition = Point.Empty;

        private System.Drawing.SolidBrush MovableBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(50, 128, 255, 128));
        private System.Drawing.SolidBrush StuckBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(50, 255, 128, 128));
        private System.Drawing.SolidBrush ShadeBrush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(100, 0, 0, 0));

        public bool Active
        {
            get
            {
                return this.active;
            }
            set
            {
                if (value != this.active)
                {
                    this.active = value;
                    this.Refresh();
                }
            }
        }

        public Point Position
        {
            get
            {
                return this.position;
            }
            set
            {
                this.position = value;
            }
        }

        public Point FinalPosition
        {
            get
            {
                return this.finalPosition;
            }
        }

        public TButton(Point position, Point finalPosition, Image image)
        {
            this.position = position;
            this.finalPosition = finalPosition;
            this.Image = image;
            this.ImageAlign = ContentAlignment.MiddleCenter;
            this.Click += new EventHandler(TButton_Click);
        }

        public void TButton_Click(object sender, EventArgs e)
        {
            if (this.CanMove)
            {
                FifteenForm form = this.Parent as FifteenForm;
                form.MoveToEmpty(this);
            }
        }
        
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);

            if (this.active) 
            {
                if (this.CanMove)
                {
                    pevent.Graphics.FillRectangle(this.MovableBrush, pevent.ClipRectangle);
                }
                else
                {
                    pevent.Graphics.FillRectangle(this.StuckBrush, pevent.ClipRectangle);
                }
            }
            else if (!this.Enabled)
            {
                pevent.Graphics.FillRectangle(this.ShadeBrush, pevent.ClipRectangle);
            }
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.active = true;
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this.active = false;
        }

        public bool CanMove
        {
            get {
                FifteenForm form = this.Parent as FifteenForm;
                return form.HasEmptyNeighbour(this.position); 
            }
        }

        public bool HasReachedFinalPosition
        {
            get
            {
                return ((this.position.X == this.finalPosition.X) && (this.position.Y == this.finalPosition.Y));
            }
        }
    }
}
